let app = new Vue({
    el: "#app",
    data: {
        up_data: [],
        datetime: "",
        inputText: "",
        defTime: "",
        oldKey: "",
        calculatingClear: null,
    },
    created: function () {
        this.reload().then(() => {
            this.calculatingTime();
        });
        this.calculatingClear = setInterval(this.calculatingTime, 1000);
    },
    mounted: function () {},
    methods: {
        send(data) {
            return window.electronAPI.sendMessage(data);
        },
        deleteUp(id) {
            let data = {
                key: `up_data.${id}`,
                db: "up_data",
                type: "store",
                cmd: "delete",
            };
            this.send(data).then((res) => {
                this.reload();
            });
            this.up_data[id].visible = false;
        },
        addUp() {
            let upId = md5(this.inputText);
            let data = {
                cmd: "set",
                type: "store",
                db: "up_data",
                key: `up_data.${upId}`,
                value: {
                    text: this.inputText,
                    datetime: this.datetime,
                    show: 0,
                    visible: false,
                },
            };

            if (!this.inputText) {
                this.$notify({
                    message: "请输入提示信息!",
                    type: "warning",
                    duration: 1000,
                });
                return;
            }
            if (!this.datetime) {
                this.$notify({
                    message: "请输入提示时间!",
                    type: "warning",
                    duration: 1000,
                });
                return;
            }
            if (this.oldKey) {
                this.deleteUp(this.oldKey);
                this.oldKey = "";
            }

            if (this.datetime && this.inputText) {
                this.send(data).then((res) => {
                    this.reload();
                });
                this.inputText = "";
            }
        },
        updateHtml(data) {
            let keys = Object.keys(data);
        },
        reload() {
            let promise = this.send({
                cmd: "getAll",
                db: "up_data",
                type: "store",
            });
            promise.then((res) => {
                this.up_data = res.data;
            });
            return promise;
        },
        back() {
            if (this.calculatingClear) {
                clearInterval(this.calculatingClear);
            }
            window.gc();
            window.location.href = "../../index.html";
        },
        reset(value, key) {
            console.log(value);
            this.inputText = value.text;
            this.datetime = value.datetime;
            this.oldKey = key;
        },
        calculatingTime() {
            this.defTime = getCurrentDate();
            let new_data = JSON.parse(JSON.stringify(this.up_data));
            for (let id in new_data) {
                let now = new Date(); //当前时间
                let ns =
                    new Date(new_data[id].datetime).getTime() - now.getTime(); //毫秒差
                if (ns > 1000) {
                    ns -= 1000; //计算出相差天数
                    let day = Math.floor(ns / (24 * 3600 * 1000)); //计算出小时数
                    let leave1 = ns % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
                    let hour = Math.floor(leave1 / (3600 * 1000)); //计算出分钟数
                    let leave2 = leave1 % (3600 * 1000); //计算小时后剩余的毫秒数
                    let minutes = Math.floor(leave2 / (60 * 1000)); //计算出秒数
                    let leave3 = leave2 % (60 * 1000); //计算分钟后剩余的毫秒数
                    let second = Math.floor(leave3 / 1000);
                    new_data[
                        id
                    ].timeLeft = `剩余：${day}天${hour}时${minutes}分${second}秒`;
                } else {
                    new_data[id].timeLeft = "剩余：0天0时0分0秒";

                    if (new_data[id].show == 0) {
                        this.reload();
                    }
                }
                this.up_data = new_data;
            }
        },
    },
});
